

package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsArticleComment;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsArticleCommentAddCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsArticleCommentCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsArticleCommentVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersDetailsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.teacher.admin.service.questions.QuestionsArticleCommentService;
import com.bei_en.teacher.admin.service.users.UsersDetailsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.bei_en.common.web.MySessionContext;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:24:22
 */
@Controller
@RequestMapping("/api/Questions")
public class QuestionsArticleCommentAPPController extends BaseController {

    @Autowired
    QuestionsArticleCommentService questionsArticleCommentService;
	@Autowired
	UsersDetailsService usersDetailsService;

    @RequestMapping("/questionsArticleComment")
	@ResponseBody
    public JsonResult questionsArticleComment(Model model,@Valid  QuestionsArticleCommentCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<QuestionsArticleCommentVo> list =new ArrayList<QuestionsArticleCommentVo>();
		   list = questionsArticleCommentService.findQuestionsArticleCommentPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   QuestionsArticleCommentVo questionsArticleCommentVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   questionsArticleCommentVo=list.get(i);
			   UsersDetailsVo usersDetailsVo = usersDetailsService
						.findById(questionsArticleCommentVo.getCommentPeople());
				if (usersDetailsVo != null) {
					if(usersDetailsVo.getRealName()!=null){
						object.put("user_name", usersDetailsVo.getRealName());
					}else{
						object.put("user_name", "易考用户");
					}
					if(usersDetailsVo.getFacePath()!=null){
						object.put("user_facepath", usersDetailsVo.getFacePath());
					}else{
						object.put("user_facepath", "");
					}
				
				} else {
					object.put("user_name", "非法用户");
					object.put("user_facepath", "");
				}
			   
			   
	   object.put("commentPeople",  questionsArticleCommentVo.getCommentPeople());
	   object.put("content",  questionsArticleCommentVo.getContent());
	   object.put("createTime",  questionsArticleCommentVo.getCreateTime().getTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/questionsArticleCommentAdd")
    @ResponseBody
    @OAuthRequired
    public JsonResult questionsArticleCommentAdd(Model model,@Valid  QuestionsArticleCommentAddCondition vo, BindingResult br, HttpSession session) {

      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
		    QuestionsArticleCommentVo questionsArticleComment=new QuestionsArticleCommentVo();
		    questionsArticleComment.setCommentPeople(vo.getCommentPeople());
		    questionsArticleComment.setContent(vo.getContent());
		    questionsArticleComment.setQuestionsArticleId(vo.getQuestionsArticleId());
		    questionsArticleComment.setCreateTime(new Date());
			questionsArticleCommentService.insert(questionsArticleComment);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

    @RequestMapping("/questionsArticleCommentEdit")
    public JsonResult questionsArticleCommentEdit(Model model,@Valid  QuestionsArticleCommentCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	QuestionsArticleCommentVo questionsArticleComment=new QuestionsArticleCommentVo();
	
		questionsArticleCommentService.updateSelective(questionsArticleComment);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
